package it.polimi.swim.entity;
import java.io.Serializable;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@NamedQueries({
	@NamedQuery(
			name="Friend.getUserFriendRequests",
			query="SELECT f FROM Friend f WHERE f.idSender = :user OR f.idReceiver = :user"),
	@NamedQuery(
			name="Friend.getAll",
			query="SELECT f FROM Friend f"),
	@NamedQuery(
			name="Friend.getFriendshipSender",
			query="SELECT u FROM SystemUser u,Friend f WHERE f.idReceiver = :user AND f.status = 'false' AND f.idSender=u.id"),
	@NamedQuery(
			name="Friend.getFriendshipReceiver",
			query="SELECT u FROM SystemUser u,Friend f WHERE f.idSender = :user AND f.status = 'false' AND f.idReceiver=u.id"),
	@NamedQuery(
		name="Friend.getUserFriend",
		query="SELECT u FROM SystemUser u, Friend f WHERE ((f.idSender = :user AND f.idReceiver = u.id) OR (f.idSender = u.id AND f.idReceiver = :user)) AND f.status = 1"),
	@NamedQuery(
			name="Friend.getFriendById",
			query="SELECT f FROM Friend f WHERE f.idSender = :idSender AND f.idReceiver = :idReceiver AND f.status = 'false'"),
	@NamedQuery(
			name="Friend.getFriendBy2Users",
			query="SELECT f FROM Friend f WHERE f.idSender = :idSender AND f.idReceiver = :idReceiver")
})

@Entity
@Table(name="Friend")
public class Friend implements Serializable{
	

	private static final long serialVersionUID = -2547263158889217678L;
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int id;
	
	@Column(name="idSender",nullable=false)
	private int idSender;
	@Column(name="idReceiver",nullable=false)
	private int idReceiver;
	//if status = true the request is accepted
	@Column(name="status")
	private boolean status;
	
	@ManyToMany(mappedBy="userFriendRequest")
	Set<SystemUser> users;
	
	public Set<SystemUser> getUsers() { return users; }
	
	//Set and Get
	public int getIdSender() {
		return idSender;
	}
	public void setIdSender(int user1) {
		this.idSender = user1;
	}
	public int getIdReceiver() {
		return idReceiver;
	}
	public void setIdReceiver(int user2) {
		this.idReceiver = user2;
	}
	public boolean isStatus() {
		return status;
	}
	public void setStatus(boolean status) {
		this.status = status;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

}
